﻿
using QLKS2014.BLL;
using QLKS2014.PUBLIC;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace QLKS2014.GUI.StupidContent
{
    /// <summary>
    /// màn hình danh sách phòng
    /// </summary>
    public partial class RoomManagement : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string _PropertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(_PropertyName));
        }
        ObservableCollection<PhongPublic> _ocPhong;

        public ObservableCollection<PhongPublic> OcPhong
        {
            get { return _ocPhong; }
            set { _ocPhong = value;
            OnPropertyChanged("OcPhong");
            }
        }


        UpdatingRoom _addingRoom;
        bool _flagAddingOrUpdatingRoom;

        public RoomManagement()
        {
            InitializeComponent();
            _initializeControl();
            Loaded += ListRoom_Loaded;
        }

        private void _initializeControl()
        {
            _addingRoom = new UpdatingRoom(this);
            ccAddingRoom.Content = _addingRoom;
        }

        void ListRoom_Loaded(object sender, RoutedEventArgs e)
        {
            initialize();
        }

        public void initialize()
        {

            List<PhongPublic> lPhong = new PhongBll().selectAll();
            if (lPhong != null)
                OcPhong = new ObservableCollection<PhongPublic>(lPhong);
            else
                OcPhong = new ObservableCollection<PhongPublic>();

            _flagAddingOrUpdatingRoom = true;
            ccAddingRoom.Visibility = Visibility.Visible;
            
        }

        private void dgRoom_KeyDown(object sender, KeyEventArgs e)
        {
            if ((Keyboard.Modifiers == ModifierKeys.Shift) && e.Key == Key.Delete)
            {
                MessageBoxResult result = MessageBox.Show("Bạn có muốn xóa phòng này không?","Nhắc Nhở", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Yes)
                {
                    DataGrid dgData = sender as DataGrid;
                    _deleteRooms(dgData);

                    // reload after deleter
                    initialize();
                }
                return;
            }
        }

        private void _deleteRooms(DataGrid dgData)
        {
            IList selectedItems = dgData.SelectedItems;
            if (selectedItems == null)
                return;
            int size = selectedItems.Count;
            for(int i = size-1; i >=0;i--)
            {
                PhongPublic item = selectedItems[i] as PhongPublic;
                new PhongBll().deleteById(item);
            }
            
        }

        private void dgRoom_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void dgRoom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PhongPublic phong0 = dgRoom.SelectedItem as PhongPublic;
            if (phong0 == null)
                return;

            ccAddingRoom.Visibility = System.Windows.Visibility.Visible;
            _addingRoom.setRoomInfo(phong0);
        }

        internal void btnAddRoom_Click_CallBack()
        {
            if (_flagAddingOrUpdatingRoom)
            {
                ccAddingRoom.Visibility = System.Windows.Visibility.Collapsed;
                _showAddingOrUpdateRoom(false);
            }
            else
            {
                ccAddingRoom.Visibility = System.Windows.Visibility.Visible;
                _showAddingOrUpdateRoom(true);
            }           
        }

        private void _showAddingOrUpdateRoom(bool show_)
        {
            if (show_)
            {

            }
            else
            {
                PhongPublic phong0 = new PhongPublic();
                
                _addingRoom.setRoomInfo(phong0);
            }

            _flagAddingOrUpdatingRoom = show_;

        }
    }
}
